﻿@model DiscountModel

@using Nop.Core.Domain.Discounts;
@using Nop.Web.Framework.UI;
@{
    Html.AddScriptParts("~/Scripts/jquery.tmpl.min.js");
}
@Html.ValidationSummary(false)
@Html.HiddenFor(model => model.Id)

<script>
    $(document).ready(function() {
        $("#discount-edit").kendoTabStrip({
            animation:  {
                open: {
                    effects: "fadeIn"
                }
            },
            select: tabstrip_on_tab_select
        });
    });
</script>
<div id="discount-edit">
    <ul>
        <li @Html.RenderSelectedTabIndex(0, GetSelectedTabIndex())>
            @T("Admin.Promotions.Discounts.Info")
        </li>
        <li @Html.RenderSelectedTabIndex(1, GetSelectedTabIndex())>
            @T("Admin.Promotions.Discounts.Requirements")
        </li>
        <li @Html.RenderSelectedTabIndex(2, GetSelectedTabIndex()) id="tab-applied-to-products">
            @T("Admin.Promotions.Discounts.AppliedToProducts")
        </li>
        <li @Html.RenderSelectedTabIndex(3, GetSelectedTabIndex()) id="tab-applied-to-categories">
            @T("Admin.Promotions.Discounts.AppliedToCategories")
        </li>
        <li @Html.RenderSelectedTabIndex(4, GetSelectedTabIndex()) id="tab-applied-to-manufacturers">
            @T("Admin.Promotions.Discounts.AppliedToManufacturers")
        </li>
        @if (Model.Id > 0)
        {
            <li @Html.RenderSelectedTabIndex(5, GetSelectedTabIndex())>
                @T("Admin.Promotions.Discounts.History")
            </li>
        }
    </ul>
    <div>
        @TabInfo()
    </div>
    <div>
        @TabRequirements()
    </div>
    <div>
        @TabAppliedToProducts()
    </div>
    <div>
        @TabAppliedToCategories()
    </div>
    <div>
        @TabAppliedToManufacturers()
    </div>
    @if (Model.Id > 0)
    {
        <div>
            @TabHistory()
        </div>
    }
</div>

@{
    //custom tabs
    var eventMessage = new AdminTabStripCreated(this.Html, "discount-edit");
    EngineContext.Current.Resolve<IEventPublisher>().Publish(eventMessage);
    foreach (var eventBlock in eventMessage.BlocksToRender)
    {
        @eventBlock
    }
}






@*save selected tab index*@
<input type="hidden" id="selected-tab-index" name="selected-tab-index" value="@(GetSelectedTabIndex())">

@helper TabInfo()
{
    <script type="text/javascript">
            $(document).ready(function () {
                $("#@Html.FieldIdFor(model => model.DiscountTypeId)").change(toggleDiscountType);
                $("#@Html.FieldIdFor(model => model.UsePercentage)").click(toggleUsePercentage);
                $("#@Html.FieldIdFor(model => model.RequiresCouponCode)").click(toggleRequiresCouponCode);
                $("#@Html.FieldIdFor(model => model.DiscountLimitationId)").change(toggleLimitation);

                toggleDiscountType();
                toggleLimitation();
                toggleUsePercentage();
                toggleRequiresCouponCode();
            });
            
            function toggleDiscountType() {
                var selectedDiscountTypeId = $("#@Html.FieldIdFor(model => model.DiscountTypeId)").val();
                if (selectedDiscountTypeId == @(((int)DiscountType.AssignedToCategories).ToString())) {
                    $('#tab-applied-to-categories').show();
                    $('#tab-applied-to-manufacturers').hide();
                    $('#tab-applied-to-products').hide();
                    $('#pnlMaximumDiscountedQuantity').show();
                    $('#pnlAppliedToSubCategories').show();
                }
                else if (selectedDiscountTypeId == @(((int)DiscountType.AssignedToManufacturers).ToString())) {
                    $('#tab-applied-to-categories').hide();
                    $('#tab-applied-to-manufacturers').show();
                    $('#tab-applied-to-products').hide();
                    $('#pnlMaximumDiscountedQuantity').show();
                    $('#pnlAppliedToSubCategories').hide();
                } else if (selectedDiscountTypeId == @(((int)DiscountType.AssignedToSkus).ToString())) {
                    $('#tab-applied-to-categories').hide();
                    $('#tab-applied-to-manufacturers').hide();
                    $('#tab-applied-to-products').show();
                    $('#pnlMaximumDiscountedQuantity').show();
                    $('#pnlAppliedToSubCategories').hide();
                }
                else {
                    $('#tab-applied-to-categories').hide();
                    $('#tab-applied-to-manufacturers').hide();
                    $('#tab-applied-to-products').hide();
                    $('#pnlMaximumDiscountedQuantity').hide();
                    $('#pnlAppliedToSubCategories').hide();
                }
            }

            function toggleLimitation() {
                var selectedDiscountLimitationId = $("#@Html.FieldIdFor(model => model.DiscountLimitationId)").val();
                if (selectedDiscountLimitationId == @(((int)DiscountLimitationType.NTimesOnly).ToString()) || selectedDiscountLimitationId == @(((int)DiscountLimitationType.NTimesPerCustomer).ToString())) {
                    //'N Times Only' or 'N Times Per Customer'
                    $('#pnlLimitationTimes').show();
                } else {
                    $('#pnlLimitationTimes').hide();
                }
            }

            function toggleUsePercentage() {
                if ($('#@Html.FieldIdFor(model => model.UsePercentage)').is(':checked')) {
                    $('#pnlDiscountPercentage').show();
                    $('#pnlDiscountAmount').hide();
                    $('#pnlMaximumDiscountAmount').show();
                } else {
                    $('#pnlDiscountPercentage').hide();
                    $('#pnlDiscountAmount').show();
                    $('#pnlMaximumDiscountAmount').hide();
                }
            }

            function toggleRequiresCouponCode() {
                if ($('#@Html.FieldIdFor(model => model.RequiresCouponCode)').is(':checked')) {
                $('#pnlCouponCode').show();
            } else {
                $('#pnlCouponCode').hide();
            }
        }
    </script>
    <table class="adminContent">
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.Name):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.Name)
                @Html.ValidationMessageFor(model => model.Name)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.DiscountTypeId):
            </td>
            <td class="adminData">
                @Html.DropDownListFor(model => model.DiscountTypeId, ((DiscountType)Model.DiscountTypeId).ToSelectList())
                @Html.ValidationMessageFor(model => model.DiscountTypeId)
            </td>
        </tr>
        <tr id="pnlAppliedToSubCategories">
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.AppliedToSubCategories):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.AppliedToSubCategories)
                @Html.ValidationMessageFor(model => model.AppliedToSubCategories)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.UsePercentage):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.UsePercentage)
                @Html.ValidationMessageFor(model => model.UsePercentage)
            </td>
        </tr>
        <tr id="pnlDiscountAmount">
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.DiscountAmount):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.DiscountAmount) [@Model.PrimaryStoreCurrencyCode]
                @Html.ValidationMessageFor(model => model.DiscountAmount)
            </td>
        </tr>
        <tr id="pnlDiscountPercentage">
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.DiscountPercentage):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.DiscountPercentage)
                @Html.ValidationMessageFor(model => model.DiscountPercentage)
            </td>
        </tr>
        <tr id="pnlMaximumDiscountAmount">
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.MaximumDiscountAmount):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.MaximumDiscountAmount) [@Model.PrimaryStoreCurrencyCode]
                @Html.ValidationMessageFor(model => model.MaximumDiscountAmount)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.StartDateUtc):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.StartDateUtc)
                @Html.ValidationMessageFor(model => model.StartDateUtc)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.EndDateUtc):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.EndDateUtc)
                @Html.ValidationMessageFor(model => model.EndDateUtc)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.RequiresCouponCode):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.RequiresCouponCode)
                @Html.ValidationMessageFor(model => model.RequiresCouponCode)
            </td>
        </tr>
        <tr id="pnlCouponCode">
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.CouponCode):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.CouponCode)
                @Html.ValidationMessageFor(model => model.CouponCode)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.DiscountLimitationId):
            </td>
            <td class="adminData">
                @Html.DropDownListFor(model => model.DiscountLimitationId, ((DiscountLimitationType)Model.DiscountLimitationId).ToSelectList())
                @Html.ValidationMessageFor(model => model.DiscountLimitationId)
            </td>
        </tr>
        <tr id="pnlLimitationTimes">
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.LimitationTimes):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.LimitationTimes)
                @Html.ValidationMessageFor(model => model.LimitationTimes) @T("Admin.Promotions.Discounts.Fields.LimitationTimes.Times")
            </td>
        </tr>
        <tr id="pnlMaximumDiscountedQuantity">
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.MaximumDiscountedQuantity):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.MaximumDiscountedQuantity)
                @Html.ValidationMessageFor(model => model.MaximumDiscountedQuantity)
            </td>
        </tr>
    </table>
}
@helper TabRequirements()
{
        if (Model.Id > 0)
        {
        
    <script type="text/javascript">
            
            $(document).ready(function () {
            
                $("#@Html.FieldIdFor(model => model.AddDiscountRequirement)").change(loadNewRequirementBox);

                //load add requirement box
                loadNewRequirementBox();

                //load existing requirements
                loadExistingRequirements();
                           
               //adding requirement event
               $("#discountRequirementContainer").bind('nopnewdiscountruleadded', function(event, newRequirementId) {
  
                   var discountId = @Model.Id;
                   
                   var postData = {
                       discountRequirementId: newRequirementId,
                       discountId: discountId
                   };
                   addAntiForgeryToken(postData);

                    $.ajax({
                        cache:false,
                        type: "POST",
                        url: "@(Url.Action("GetDiscountRequirementMetaInfo", "Discount"))",
                        data: postData,
                        success: function (result) {                            
                            discountRequirements.push({ discountRequirementId: newRequirementId, ruleName : result.ruleName, url : result.url });
                            loadExistingRequirements();
                        },
                        error:function (xhr, ajaxOptions, thrownError) {
                            alert('Failed to load new discount requirement info. Please refresh the page');
                        }
                    });

               });
            });

                
            //load requirement HTML
            function loadRequirement(configureRuleUrl, placeholderName) {
                $.ajax({
                    cache:false,
                    url: configureRuleUrl,
                    dataType: "html",
                    success: function (result) {
                        $(placeholderName).html(result);
                    },
                    error:function (xhr, ajaxOptions, thrownError) {
                        $(placeholderName).html('Failed to load requirement content.');
                    }
                });
            }

            //load HTML for adding requirement
            function loadNewRequirementBox() {
                //load URL
                var ruleSystemName = $("#@Html.FieldIdFor(model => model.AddDiscountRequirement)").val();
                var discountId = @Model.Id;
                if (ruleSystemName != '') {
                    $.ajax({
                        cache:false,
                        url: "@(Url.Action("GetDiscountRequirementConfigurationUrl", "Discount"))",
                        data: { "systemName": ruleSystemName, "discountId": discountId },
                        success: function (result) {
                                var configureRuleUrl = result.url;
                                //now load the content
                                loadRequirement(configureRuleUrl, '#addrequirementplaceholder');
                        },
                        error:function (xhr, ajaxOptions, thrownError){
                            $('#addrequirementplaceholder').html('Failed to load requirement URL');
                        }  
                    });
                }
                else {
                    $('#addrequirementplaceholder').html('');
                }
            }

           //array of exisiting discount requirements
           var discountRequirements = [
                    @for (int i = 0; i < Model.DiscountRequirementMetaInfos.Count; i++)
                    {
                        var drmi = Model.DiscountRequirementMetaInfos[i];
                        <text>{ discountRequirementId: @(drmi.DiscountRequirementId), ruleName: "@(drmi.RuleName)", url:  "@(Html.Raw(drmi.ConfigurationUrl))" } </text> if (i != Model.DiscountRequirementMetaInfos.Count - 1)
                                                                                                                                                                         {<text>, </text>}

                    }
               ];
           
           //load HTML for exisiting requirements
           function loadExistingRequirements() {
               $("#discountRequirementContainer").html('');
               $("#discountRequirementTemplate").tmpl(discountRequirements).appendTo("#discountRequirementContainer");

               var discountRequirementsLen = discountRequirements.length;
               for (var i=0; i<discountRequirementsLen; i++) {
                   var url = discountRequirements[i].url;
                   var discountRequirementId = discountRequirements[i].discountRequirementId;
                   var placeholderName = '#editrequirementplaceholder' + discountRequirementId;
                   $(placeholderName).html('loading...');
                   loadRequirement(url, placeholderName);
               }
           }

           //delete requirement rule
           function deleteRequirement(discountRequirementId) {           
               var discountId = @Model.Id;
               
               var postData = {
                   discountRequirementId: discountRequirementId,
                   discountId: discountId
               };
               addAntiForgeryToken(postData);

                $.ajax({
                    cache: false,
                    type: "POST",
                    url: "@(Url.Action("DeleteDiscountRequirement", "Discount"))",
                    data: postData,
                    success: function (data) {
                        removeRequirementByElement(discountRequirements, discountRequirementId);
                        loadExistingRequirements();
                    },
                    error:function (xhr, ajaxOptions, thrownError){
                        alert('Failed to delete requirement.');
                    }
                });

                
            function removeRequirementByElement(arrayName,discountRequirementId) {
                for(var i=0; i<arrayName.length; i++) {
                    if(arrayName[i].discountRequirementId==discountRequirementId) {
                        arrayName.splice(i,1);
                    }
                }
             }

        }

    </script>
    <!-- Template container -->
    <div id="discountRequirementContainer">
    </div>
    <!-- Discount requirement template -->
    <script id="discountRequirementTemplate" type="text/html">
            <div id="editrequirement{{= discountRequirementId }}">
                <div>@T("Admin.Promotions.Discounts.Requirements.DiscountRequirementType"): <strong>{{= ruleName }}</strong></div>
                <div id="editrequirementplaceholder{{= discountRequirementId }}" style="float:left;"></div>
                <div style="float:right;">
                    <input type="button" id="deleterequirement{{= discountRequirementId }}" onclick="deleteRequirement({{= discountRequirementId }})" class="k-button" value="@T("Admin.Promotions.Discounts.Requirements.Remove")" />
                </div>
                <table class="adminContent">
                    <tr class="adminSeparator">
                        <td colspan="2">
                            <hr />
                        </td>
                    </tr>
                </table>
            </div>
    </script>
            
    <table class="adminContent">
        <tr>
            <td colspan="2">
                <strong>@T("Admin.Promotions.Discounts.Requirements.AddNew")</strong>
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.AddDiscountRequirement):
            </td>
            <td class="adminData">
                @Html.DropDownListFor(model => model.AddDiscountRequirement, Model.AvailableDiscountRequirementRules)
                @Html.ValidationMessageFor(model => model.AddDiscountRequirement)
            </td>
        </tr>
    </table>
    <table class="adminContent">
        <tr>
            <td colspan="2">
                <div id="addrequirementplaceholder">
                </div>
            </td>
        </tr>
    </table>
        }
        else
        {
    @T("Admin.Promotions.Discounts.Requirements.SaveBeforeEdit")
        }
}
@helper TabAppliedToProducts()
{
    if (Model.Id > 0)
    {
        <table class="adminContent">
            <tr>
                <td>

                    <div id="products-grid"></div>

                    <script>
                        $(document).ready(function () {
                            $("#products-grid").kendoGrid({
                                dataSource: {
                                    type: "json",
                                    transport: {
                                        read: {
                                            url: "@Html.Raw(Url.Action("ProductList", "Discount", new { discountId = Model.Id }))",
                                            type: "POST",
                                            dataType: "json",
                                            data: addAntiForgeryToken
                                        },
                                        destroy: {
                                            url: "@Html.Raw(Url.Action("ProductDelete", "Discount", new { discountId = Model.Id }))",
                                            type: "POST",
                                            dataType: "json",
                                            data: addAntiForgeryToken
                                        }
                                    },
                                    schema: {
                                        data: "Data",
                                        total: "Total",
                                        errors: "Errors",
                                        model: {
                                            id: "ProductId",
                                            fields: {
                                                ProductName: {editable: false, type: "string" },
                                                ProductId: {editable: false, type: "number" }
                                            }
                                        }
                                    },
                                    error: function(e) {
                                        display_kendoui_grid_error(e);
                                        // Cancel the changes
                                        this.cancelChanges();
                                    },
                                    serverPaging: true,
                                    serverFiltering: true,
                                    serverSorting: true
                                },
                                pageable: {
                                    refresh: true,
                                    numeric: false,
                                    previousNext: false,
                                    info: false
                                },
                                editable: {
                                    confirmation: false,
                                    mode: "inline"
                                },
                                scrollable: false,
                                columns: [{
                                    field: "ProductName",
                                    title: "@T("Admin.Promotions.Discounts.AppliedToProducts.Product")"
                                },  {
                                    field: "ProductId",
                                    title: "@T("Admin.Common.View")",
                                    width: 150,
                                    template: '<a href="@Url.Content("~/Admin/Product/Edit/")#=ProductId#">@T("Admin.Common.View")</a>'
                                }, {
                                    command: [{
                                        name: "destroy",
                                        text: "@T("Admin.Common.Delete")"
                                    }],
                                    width: 150
                                }]
                            });
                        });
                    </script>
                </td>
            </tr>
            <tr>
                <td width="100%">
                    <input type="submit" id="btnAddNewProduct" value="@T("Admin.Promotions.Discounts.AppliedToProducts.AddNew")" onclick="javascript:OpenWindow('@(Url.Action("ProductAddPopup", "Discount", new { discountId = Model.Id, btnId = "btnRefreshProducts", formId = "discount-form" }))', 800, 800, true); return false;" class="k-button" />
                    <input type="submit" id="btnRefreshProducts" style="display: none" />
                    <script type="text/javascript">
                        $(document).ready(function () {
                            $('#btnRefreshProducts').click(function () {
                                //refresh grid
                                var grid = $("#products-grid").data('kendoGrid');
                                grid.dataSource.read();

                                //return false to don't reload a page
                                return false;
                            });
                        });
                    </script>
                </td>
            </tr>
        </table>
    }
    else
    {
        @T("Admin.Promotions.Discounts.AppliedToProducts.SaveBeforeEdit")
    }
}
@helper TabAppliedToCategories()
{
    if (Model.Id > 0)
    {
        <table class="adminContent">
            <tr>
                <td>

                    <div id="categories-grid"></div>

                    <script>
                        $(document).ready(function () {
                            $("#categories-grid").kendoGrid({
                                dataSource: {
                                    type: "json",
                                    transport: {
                                        read: {
                                            url: "@Html.Raw(Url.Action("CategoryList", "Discount", new { discountId = Model.Id }))",
                                            type: "POST",
                                            dataType: "json",
                                            data: addAntiForgeryToken
                                        },
                                        destroy: {
                                            url: "@Html.Raw(Url.Action("CategoryDelete", "Discount", new { discountId = Model.Id }))",
                                            type: "POST",
                                            dataType: "json",
                                            data: addAntiForgeryToken
                                        }
                                    },
                                    schema: {
                                        data: "Data",
                                        total: "Total",
                                        errors: "Errors",
                                        model: {
                                            id: "CategoryId",
                                            fields: {
                                                CategoryName: {editable: false, type: "string" },
                                                CategoryId: {editable: false, type: "number" }
                                            }
                                        }
                                    },
                                    error: function(e) {
                                        display_kendoui_grid_error(e);
                                        // Cancel the changes
                                        this.cancelChanges();
                                    },
                                    serverPaging: true,
                                    serverFiltering: true,
                                    serverSorting: true
                                },
                                pageable: {
                                    refresh: true,
                                    numeric: false,
                                    previousNext: false,
                                    info: false
                                },
                                editable: {
                                    confirmation: false,
                                    mode: "inline"
                                },
                                scrollable: false,
                                columns: [{
                                    field: "CategoryName",
                                    title: "@T("Admin.Promotions.Discounts.AppliedToCategories.Category")"
                                },  {
                                    field: "CategoryId",
                                    title: "@T("Admin.Common.View")",
                                    width: 150,
                                    template: '<a href="@Url.Content("~/Admin/Category/Edit/")#=CategoryId#">@T("Admin.Common.View")</a>'
                                }, {
                                    command: [{
                                        name: "destroy",
                                        text: "@T("Admin.Common.Delete")"
                                    }],
                                    width: 150
                                }]
                            });
                        });
                    </script>
                </td>
            </tr>
            <tr>
                <td width="100%">
                    <input type="submit" id="btnAddNewCategory" value="@T("Admin.Promotions.Discounts.AppliedToCategories.AddNew")" onclick="javascript:OpenWindow('@(Url.Action("CategoryAddPopup", "Discount", new { discountId = Model.Id, btnId = "btnRefreshCategories", formId = "discount-form" }))', 800, 800, true); return false;" class="k-button" />
                    <input type="submit" id="btnRefreshCategories" style="display: none" />
                    <script type="text/javascript">
                        $(document).ready(function () {
                            $('#btnRefreshCategories').click(function () {
                                //refresh grid
                                var grid = $("#categories-grid").data('kendoGrid');
                                grid.dataSource.read();

                                //return false to don't reload a page
                                return false;
                            });
                        });
                    </script>
                </td>
            </tr>
        </table>
    }
    else
    {
        @T("Admin.Promotions.Discounts.AppliedToCategories.SaveBeforeEdit")
    }
}
@helper TabAppliedToManufacturers()
{
    if (Model.Id > 0)
    {
        <table class="adminContent">
            <tr>
                <td>

                    <div id="manufacturers-grid"></div>

                    <script>
                        $(document).ready(function () {
                            $("#manufacturers-grid").kendoGrid({
                                dataSource: {
                                    type: "json",
                                    transport: {
                                        read: {
                                            url: "@Html.Raw(Url.Action("ManufacturerList", "Discount", new { discountId = Model.Id }))",
                                            type: "POST",
                                            dataType: "json",
                                            data: addAntiForgeryToken
                                        },
                                        destroy: {
                                            url: "@Html.Raw(Url.Action("ManufacturerDelete", "Discount", new { discountId = Model.Id }))",
                                            type: "POST",
                                            dataType: "json",
                                            data: addAntiForgeryToken
                                        }
                                    },
                                    schema: {
                                        data: "Data",
                                        total: "Total",
                                        errors: "Errors",
                                        model: {
                                            id: "ManufacturerId",
                                            fields: {
                                                ManufacturerName: {editable: false, type: "string" },
                                                ManufacturerId: {editable: false, type: "number" }
                                            }
                                        }
                                    },
                                    error: function(e) {
                                        display_kendoui_grid_error(e);
                                        // Cancel the changes
                                        this.cancelChanges();
                                    },
                                    serverPaging: true,
                                    serverFiltering: true,
                                    serverSorting: true
                                },
                                pageable: {
                                    refresh: true,
                                    numeric: false,
                                    previousNext: false,
                                    info: false
                                },
                                editable: {
                                    confirmation: false,
                                    mode: "inline"
                                },
                                scrollable: false,
                                columns: [{
                                    field: "ManufacturerName",
                                    title: "@T("Admin.Promotions.Discounts.AppliedToManufacturers.Manufacturer")"
                                },  {
                                    field: "ManufacturerId",
                                    title: "@T("Admin.Common.View")",
                                    width: 150,
                                    template: '<a href="@Url.Content("~/Admin/Manufacturer/Edit/")#=ManufacturerId#">@T("Admin.Common.View")</a>'
                                }, {
                                    command: [{
                                        name: "destroy",
                                        text: "@T("Admin.Common.Delete")"
                                    }],
                                    width: 150
                                }]
                            });
                        });
                    </script>
                </td>
            </tr>
            <tr>
                <td width="100%">
                    <input type="submit" id="btnAddNewManufacturer" value="@T("Admin.Promotions.Discounts.AppliedToManufacturers.AddNew")" onclick="javascript:OpenWindow('@(Url.Action("ManufacturerAddPopup", "Discount", new { discountId = Model.Id, btnId = "btnRefreshManufacturers", formId = "discount-form" }))', 800, 800, true); return false;" class="k-button" />
                    <input type="submit" id="btnRefreshManufacturers" style="display: none" />
                    <script type="text/javascript">
                        $(document).ready(function () {
                            $('#btnRefreshManufacturers').click(function () {
                                //refresh grid
                                var grid = $("#manufacturers-grid").data('kendoGrid');
                                grid.dataSource.read();

                                //return false to don't reload a page
                                return false;
                            });
                        });
                    </script>
                </td>
            </tr>
        </table>
    }
    else
    {
        @T("Admin.Promotions.Discounts.AppliedToManufacturers.SaveBeforeEdit")
    }
}
@helper TabHistory()
{
        var defaultGridPageSize = EngineContext.Current.Resolve<Nop.Core.Domain.Common.AdminAreaSettings>().DefaultGridPageSize;
        var gridPageSizes = EngineContext.Current.Resolve<Nop.Core.Domain.Common.AdminAreaSettings>().GridPageSizes;

    <div id="usagehistory-grid"></div>

    <script>
        $(document).ready(function () {
            $("#usagehistory-grid").kendoGrid({
                dataSource: {
                    type: "json",
                    transport: {
                        read: {
                            url: "@Html.Raw(Url.Action("UsageHistoryList", "Discount", new { discountId = Model.Id }))",
                            type: "POST",
                            dataType: "json",
                            data: addAntiForgeryToken
                        },
                        destroy: {
                            url: "@Html.Raw(Url.Action("UsageHistoryDelete", "Discount"))",
                            type: "POST",
                            dataType: "json",
                            data: addAntiForgeryToken
                        }
                    },
                    schema: {
                        data: "Data",
                        total: "Total",
                        errors: "Errors",
                        model: {
                            id: "Id",
                            fields: {
                                //do not implicitly specify all fields
                                //we do it only for fields which implicitly require it
                                //otherwise, they'll be formatted wrong way
                                CreatedOn: {type: "date" }
                            }
                        }
                    },
                    error: function(e) {
                        display_kendoui_grid_error(e);
                        // Cancel the changes
                        this.cancelChanges();
                    },
                    pageSize: @(defaultGridPageSize),
                    serverPaging: true,
                    serverFiltering: true,
                    serverSorting: true
                },
                pageable: {
                    refresh: true,
                    pageSizes: [@(gridPageSizes)]
                },
                editable: {
                    confirmation: false,
                    mode: "inline"
                },
                scrollable: false,
                columns: [{
                    field: "CreatedOn",
                    title: "@T("Admin.Promotions.Discounts.History.CreatedOn")",
                    width: 200,
                    type: "date",
                    format: "{0:G}"
                }, {
                    field: "OrderId",
                    title: "@T("Admin.Promotions.Discounts.History.Order")",
                    width: 200,
                    template: '<a href="@Url.Content("~/Admin/Order/Edit/")#=OrderId#">#=OrderId# - @T("Admin.Common.View")</a>'
                }, {
                    field: "OrderTotal",
                    title: "@T("Admin.Promotions.Discounts.History.OrderTotal")",
                    width: 200
                },{
                    command: {name: "destroy", text: "@T("Admin.Common.Delete")"},
                    title: "@T("Admin.Common.Delete")",
                    width: 100
                }]
            });
        });
    </script>
}